package com.hhd.form.table.db;

import com.hhd.form.component.ComponentDBFieldType;
import com.hhd.form.component.FieldComponentDelegate;
import com.hhd.form.entity.DesignFormDefinition;
import com.hhd.form.entity.DesignFormFieldDefinition;
import com.hhd.form.entity.DesignFormTableFieldMeta;
import com.hhd.form.entity.DesignFormTableMeta;
import com.hhd.form.table.TableDefinition;
import com.hhd.form.table.field.TableFieldDefinition;
import com.hhd.form.vo.FormDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DBOperator {
    private static DBType dbType = DBType.mysql;

    @Autowired
    FieldComponentDelegate fieldComponent;

    @Autowired
    List<Operator> operators;

    public static void setDBType(DBType dbType) {
        DBOperator.dbType = dbType;
    }

    public void doCreateTable(DesignFormTableMeta tableMeta, DesignFormDefinition formDefinition) {
        TableDefinition tableDefinition = new TableDefinition();
        tableDefinition.setTableName(tableMeta.getTableName());
        tableDefinition.setComment(tableMeta.getComment());
        getOperator().doCreateTable(tableDefinition);
    }

    public void updateTableComment(FormDefinition formDefinition, DesignFormTableMeta designFormTableMeta) {
        TableDefinition tableDefinition = new TableDefinition();
        tableDefinition.setTableName(formDefinition.getTableName());
        tableDefinition.setComment(formDefinition.getRemark());
        getOperator().updateTableComment(tableDefinition);
    }

    public void doCreateTableField(DesignFormFieldDefinition field, DesignFormTableFieldMeta fieldMeta, DesignFormTableMeta tableMeta, FormDefinition fomDefinition) {
        List<TableFieldDefinition> tableFields = fieldComponent.onCreateField(field, fieldMeta, fomDefinition, tableMeta);
        for (TableFieldDefinition tableField : tableFields) {
            tableField.setTableName(tableMeta.getTableName());
            getOperator().doCreateTableField(tableField);
        }
    }

    public void updateFieldComment(DesignFormFieldDefinition field, DesignFormTableFieldMeta designFormTableFieldMeta, DesignFormTableMeta tableMeta) {
        TableFieldDefinition fieldDefinition = new TableFieldDefinition();
        fieldDefinition.setFieldName(field.getFieldName());
        fieldDefinition.setComment(field.getLabel());
        fieldDefinition.setTableName(tableMeta.getTableName());
        fieldDefinition.setDbType(ComponentDBFieldType.forName(designFormTableFieldMeta.getFieldType()));
        fieldDefinition.setLength(field.getLength());
        getOperator().updateFieldComment(fieldDefinition);
    }

    private Operator getOperator(DBType dbType) {
        for (Operator operator : operators) {
            if (operator.getDBType() == dbType) {
                return operator;
            }
        }
        throw new RuntimeException("动态表单不支持的数据库类型: " + dbType.toString());
    }

    private Operator getOperator() {
        return getOperator(dbType);
    }
}
